package com.gjy.ignite.util;

import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.cluster.ClusterState;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

import java.util.Collections;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-08-09 10:02:37
 */
public class IgniteFactory {
    private static Ignite ignite;

    public static Ignite get() {
        if (ignite == null) {
            IgniteConfiguration cfg = new IgniteConfiguration();
            cfg.setClientMode(true);                                      // 纯客户端
            TcpDiscoverySpi spi = new TcpDiscoverySpi();
            TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
            ipFinder.setAddresses(Collections.singletonList("192.168.253.142:47500..47509"));
            spi.setIpFinder(ipFinder);
            cfg.setDiscoverySpi(spi);
            cfg.setPeerClassLoadingEnabled(true);                         // 计算任务用
            ignite = Ignition.start(cfg);
            ignite.cluster().state(ClusterState.ACTIVE);                  // 激活集群
        }
        return ignite;
    }
}
